\section{Decoding using Matlab}
\label{sec:matlabdecoding}

As the demodulator wasn't ready in time all Matlab code has been written against ideal signals but can easily be modified for non-ideal signals.

The following function was used to generate a Manchester encoded signal. It expects the data as input and will output both the signal as well as a timing matrix which can be used for plotting.
\lstinputlisting[language=Matlab]{Code/ManchesterSignal.m}

Edge detection using Matlab is trivial by looking at the differential of the input signal. Falling edges are where this differential is less than 0. This function returns the period between two edges rounded to one decimal place. The index of the edges is returned for debugging purposes.
\lstinputlisting[language=Matlab]{Code/ManchesterPeriod.m}

The signal is reconstructed by examining the periods between two falling edges and the last bit of the sequence as per \cref{tab:manchesterdecoding}. This is implemented in the following function.

\lstinputlisting[language=Matlab]{Code/ManchesterDecode.m}

\begin{table}
%\renewcommand{\arraystretch}{1.3}
\centering
\begin{tabular}{| l | l | l |}
\hline
\textbf{Periods} & \textbf{Previous Bit} & \textbf{Next bit(s)} \\ \hline
1 & 0 & 0  \\
  & 1 & 1  \\ \hline
1.5 & 1 & 0 \\
    & 0 & 01 \\ \hline
2 & 1 & 01 \\
  & 0 & 10 \\ 
\hline
\end{tabular}
\caption{Translating measured periods to bits for Manchester decoding}
\label{tab:manchesterdecoding}
\end{table}
